package com.zzb.projectfactory.framework.dict.controller;

import com.zzb.projectfactory.framework.common.BaseController;
import com.zzb.projectfactory.framework.dict.entity.SysDict;
import com.zzb.projectfactory.framework.role.entity.User;
import com.zzb.projectfactory.framework.dict.form.SysDictSearchForm;
import com.zzb.projectfactory.framework.dict.service.SysDictService;
import com.zzb.projectfactory.framework.common.BasePage;
import com.zzb.projectfactory.framework.common.BaseResponse;
import com.zzb.projectfactory.framework.common.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
* 数据字典controller
*/
@Controller
@RequestMapping("/system/sysDict")
public class SysDictController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SysDictController.class);
    @Autowired
    private SysDictService sysDictService;

    /**数据字典管理首页*/
    @RequestMapping({"","/","/index"})
    public String index(){
        return "system/sysDict/index";
    }
    /**数据字典列表json*/
    @RequestMapping("list")
    @ResponseBody
    public BasePage<SysDict> list(SysDictSearchForm sysDictSearchForm){
        return sysDictService.queryPage(sysDictSearchForm, SysDict.class);
    }

    /**数据字典表单页面*/
    @RequestMapping("form")
    public String form(String id, Model model){
        SysDict sysDict;
        if(StringUtils.isEmpty(id)){
            sysDict = new SysDict();
    } else{
        sysDict = sysDictService.get(id);
        if(sysDict == null) sysDict = new SysDict();
    }
        model.addAttribute("sysDict",sysDict);
        return "system/sysDict/form";
    }

    /**数据字典保存*/
    @RequestMapping("/save")
    @ResponseBody
    public BaseResponse<SysDict> save(SysDict sysDict , HttpServletRequest request){
        try {
            User user = getCurrentUser(request);
            return sysDictService.saveSysDict(sysDict, user);
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse<>(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**数据字典删除*/
    @RequestMapping("/delete")
    @ResponseBody
    public BaseResponse delete(String id,HttpServletRequest request){
        try {
            return sysDictService.deleteSysDict(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**数据字典开启*/
    @RequestMapping("/open")
    @ResponseBody
    public BaseResponse open(String id ,HttpServletRequest request){
        try {
            return sysDictService.recovery(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

    /**数据字典屏蔽*/
    @RequestMapping("/deny")
    @ResponseBody
    public BaseResponse deny(String id ,HttpServletRequest request){
        try {
            return sysDictService.trash(id,getCurrentUser(request));
        } catch (ServiceException e) {
            e.printStackTrace();
            return new BaseResponse(BaseResponse.FAIL,e.getMessage());
        }
    }

}
